package com.example.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.WorkOrderMapper;
import com.example.demo.model.po.WorkOrder;
import com.example.demo.model.vo.WorkOrderVo;
import com.example.demo.service.WorkOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.*;
import java.util.List;

@Service("workOrderService")
public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder> implements WorkOrderService {

    @Resource
    private WorkOrderMapper workOrderMapper;

        @Override
    public boolean save(WorkOrder workOrder) {//添加
        List<WorkOrder> workOrderList = workOrderMapper.findAll(workOrder.getOrderNo());
        if (workOrderList.size()!=0){
            throw new RuntimeException("输入的工单编号重复，请检查");
        }else {
            workOrderMapper.insert(workOrder);
            return true;
        }
    }


    @Override
    public boolean removeById(Long id) {//删除
        workOrderMapper.deleteById(id);
        return true;
    }

    @Override
    public boolean updateById(WorkOrder workOrder) {//修改
        List<WorkOrder> workOrderList = workOrderMapper.findAll(workOrder.getOrderNo());
        if (workOrderList.size()!=0){
            throw new RuntimeException("输入的工单编号重复，请检查");
        }else {
            workOrderMapper.updateById(workOrder);
            return true;
        }
    }

    @Override
    public Page<WorkOrder> page(Page<WorkOrder> page, QueryWrapper<WorkOrder> queryWrapper) {
        return workOrderMapper.selectPage(page,queryWrapper);
    }

    @Override
    public boolean updateByFenpai(WorkOrder workOrder) {
        workOrderMapper.updateById(workOrder);
        return true;
    }

    public List<WorkOrderVo> getDepartmentStatsForJuly() {//查询7月每个部门的工单总量、超期率
        return workOrderMapper.selectDepartmentStatsForJuly();
    }

    public List<WorkOrderVo> getOrderTypeStatsForJuly() {//查询7月每个工单类型的工单总量、超期率
        List<WorkOrderVo> workOrderVoList =workOrderMapper.selectOrderTypeStatsForJuly();
        return workOrderVoList;
    }


}
